package com.engine4cloud.tdf.supervision.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.engine4cloud.tdf.admin.api.feign.RemoteDeptService;
import com.engine4cloud.tdf.common.core.constant.SecurityConstants;
import com.engine4cloud.tdf.common.core.exception.ValidateCodeException;
import com.engine4cloud.tdf.common.core.util.R;
import com.engine4cloud.tdf.common.core.util.RetOps;
import com.engine4cloud.tdf.common.data.web.query.Pages;
import com.engine4cloud.tdf.common.data.web.query.Queries;
import com.engine4cloud.tdf.common.data.web.response.PageInfo;
import com.engine4cloud.tdf.common.security.service.TdfUser;
import com.engine4cloud.tdf.common.security.util.SecurityUtils;
import com.engine4cloud.tdf.supervision.convert.JzdxFeatureConvert;
import com.engine4cloud.tdf.supervision.dto.create.TerminalBiologyCreateDTO;
import com.engine4cloud.tdf.supervision.dto.query.TerminalBiologyQueryDTO;
import com.engine4cloud.tdf.supervision.dto.result.JzdxTerminalBiologyResultDTO;
import com.engine4cloud.tdf.supervision.dto.result.TerminalBiologyResultDTO;
import com.engine4cloud.tdf.supervision.dto.update.TerminalBiologyUpdateDTO;
import com.engine4cloud.tdf.supervision.entity.JzdxBiologyFeatureEntity;
import com.engine4cloud.tdf.supervision.mapper.JzdxBiologyFeatureMapper;
import com.engine4cloud.tdf.supervision.service.JzdxBiologyFeatureService;
import jakarta.annotation.Resource;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Objects;

/**
 * 矫正对象人脸特征信息
 *
 * @author wenjie
 * @date 2024-07-19 17:23:10
 */
@Service
public class JzdxBiologyFeatureServiceImpl extends ServiceImpl<JzdxBiologyFeatureMapper, JzdxBiologyFeatureEntity> implements JzdxBiologyFeatureService {
    @Resource
    private RemoteDeptService remoteDeptService;

    @Override
    public TerminalBiologyResultDTO findOne(String identity) {
        return JzdxFeatureConvert.INSTANCE.convert(super.getOne(wrapper(identity)));
    }

    @Override
    public Boolean create(TerminalBiologyCreateDTO dto) {
        return super.save(JzdxFeatureConvert.INSTANCE.create(dto, SecurityUtils.getUser().getTenantId()));
    }

    @Override
    public Boolean update(TerminalBiologyUpdateDTO dto) {
        return super.updateById(JzdxFeatureConvert.INSTANCE.update(dto));
    }

    @Override
    public Boolean createOrUpdate(TerminalBiologyCreateDTO dto) {
        TerminalBiologyResultDTO old = this.findOne(dto.getIdentity());
        if (Objects.nonNull(old)) {
            return super.updateById(JzdxFeatureConvert.INSTANCE.createOrUpdate(dto, old.getId()));
        }

        return super.save(JzdxFeatureConvert.INSTANCE.create(dto, SecurityUtils.getUser().getTenantId()));
    }

    @Override
    public Boolean updateOrCreate(TerminalBiologyUpdateDTO dto) {
        if (Objects.nonNull(dto.getId())) {
            return super.updateById(JzdxFeatureConvert.INSTANCE.update(dto));
        }

        TdfUser user = SecurityUtils.getUser();
        return super.save(JzdxFeatureConvert.INSTANCE.updateOrCreate(dto, user.getTenantId()));
    }

    @Override
    public PageInfo<JzdxTerminalBiologyResultDTO> pageList(TerminalBiologyQueryDTO dto) {
        R<List<Long>> deptIdRemoteList = remoteDeptService.getDescendantIdList(dto.getDeptId(), SecurityConstants.FROM_IN);

        List<Long> deptIdList = RetOps.of(deptIdRemoteList).getData().filter(CollectionUtils::isNotEmpty)
                .orElseThrow(() -> new ValidateCodeException("没有数据权限!"));

        IPage<JzdxTerminalBiologyResultDTO> pages = this.baseMapper.pageList(Pages.page(dto), deptIdList);
        return Pages.convert(pages);
    }

    private LambdaQueryWrapper<JzdxBiologyFeatureEntity> wrapper(String identity) {
        LambdaQueryWrapper<JzdxBiologyFeatureEntity> wrapper = Queries.lambda();
        Queries.accept(identity, v -> wrapper.eq(JzdxBiologyFeatureEntity::getIdentity, v));
        return wrapper;
    }
}